#include "MainMenuScene.h"
#include "GamePlayScene.h"

USING_NS_CC;

MainMenuScene::MainMenuScene():
__calibrationValues(b2Vec3(0,1,1)),	//nhandt  calibrationValues
__winSize()
{
}

/**
 * Call this method to create the MainMenuScene.
 */
CCScene*
MainMenuScene::scene()
{
	CCScene * scene = NULL;
    do 
    {
        // 'scene' is an autorelease object
        scene = CCScene::create();
        CC_BREAK_IF(! scene);

        // 'layer' is an autorelease object
        MainMenuScene *layer = MainMenuScene::create();
        CC_BREAK_IF(! layer);

        // add layer as a child to scene
        scene->addChild(layer);
    } while (0);

    // return the scene
    return scene;
}

bool
MainMenuScene::init()
{
	// Init super first.
	if ( !CCLayer::init() )
	{
		return false;
	}

	__winSize = CCDirector::sharedDirector()->getWinSize();

	// Create the background sprite.
	CCSprite* background = CCSprite::spriteWithFile("menuBackground.png");

	// Change the anchor for convenience.
	//pBackground->setAnchorPoint(ccp(0, 0));
	background->setPosition(ccp(__winSize.width/2, __winSize.height/2));

	float scaleX = __winSize.width/ background->getTextureRect().size.width;
	float scaleY = __winSize.height/ background->getTextureRect().size.height;
	background->setScaleX(scaleX);
	background->setScaleY(scaleY);

	this->addChild(background, -1);

	CCMenuItem* playButton = createButton("Play", menu_selector(MainMenuScene::menuStartCallback), 0);
	CCMenuItem* scoresButton = createButton("Scores", menu_selector(MainMenuScene::menuScoresCallback), 1);
	CCMenuItem* exitButton = createButton("Exit", menu_selector(MainMenuScene::menuExitCallback), 2);
	CCMenuItem* infoButton = createInfoButton();

	// Create menu, it's an autorelease object
	CCMenu* pMenu = CCMenu::menuWithItems(playButton, scoresButton, exitButton, infoButton, NULL);
	pMenu->setPosition(CCPointZero);
	this->addChild(pMenu, 1);

	return true;
}

CCMenuItem*
MainMenuScene::createButton(const char* label, SEL_MenuHandler selector, int position)
{
	CCMenuItem* button = CCMenuItemImage::itemWithNormalImage("menuButton.png",
															  "menuButton.png",
															  this,
															  selector);
	CCLabelTTF* buttonLabel = CCLabelTTF::labelWithString(label, "Sansation_Bold.ttf", 35.0f);
	buttonLabel->setColor(ccc3(0.0f, 0.0f, 0.0f));

	CCSize buttonSize = button->getContentSize();
	float playButtonX = __winSize.width - 30.0f - buttonSize.width / 2.0f;
	float playButtonY = __winSize.height - 40.0f - buttonSize.height / 2.0f - position * (buttonSize.height + 8.0f);

	button->setPosition(playButtonX, playButtonY);
	button->addChild(buttonLabel);
	buttonLabel->setPosition(ccp(buttonSize.width / 2.0f, buttonSize.height / 2.0f));

	return button;
}

CCMenuItem*
MainMenuScene::createInfoButton()
{
	CCMenuItem* button = CCMenuItemImage::itemWithNormalImage("infoButton.png",
															  "infoButton.png",
															  this,
															  menu_selector(MainMenuScene::menuInfoCallback));
	CCLabelTTF* buttonLabel = CCLabelTTF::labelWithString("i", "fonts/Sansation/Sansation_Bold.ttf", 45.0f);
	buttonLabel->setColor(ccc3(0.0f, 0.0f, 0.0f));

	CCSize buttonSize = button->getContentSize();
	button->setPosition(30.0f, 30.0f);
	button->addChild(buttonLabel);
	buttonLabel->setPosition(ccp(buttonSize.width / 2.0f, buttonSize.height / 2.0f));

	return button;
}

void
MainMenuScene::menuStartCallback(cocos2d::CCObject* pSender)
{
	CCScene *pScene = GamePlayScene::scene(__calibrationValues);
	CCDirector::sharedDirector()->replaceScene(pScene);
}

void
MainMenuScene::menuScoresCallback(cocos2d::CCObject* pSender)
{
	//CCScene* pHighScoresScene = HighScoresScene::scene();
	//CCDirector::sharedDirector()->replaceScene(pHighScoresScene);
}

void
MainMenuScene::menuInfoCallback(cocos2d::CCObject* pSender)
{
	//CCScene* pInfoScene = InfoScene::scene();
	//CCDirector::sharedDirector()->replaceScene(pInfoScene);
}

void
MainMenuScene::menuExitCallback(cocos2d::CCObject* pSender)
{
	CCDirector::sharedDirector()->end();
}

void
MainMenuScene::onEnter()
{
	CCLayer::onEnter();

	//Start reading accelerometer values.
	this->setAccelerometerEnabled(true);
}

void
MainMenuScene::didAccelerate(CCAcceleration* pAccelerationValue)
{
	/*
	//nhandt static set AccelerationValue from main menu
	__calibrationValues.x = pAccelerationValue->x;
	__calibrationValues.y = pAccelerationValue->y;
	__calibrationValues.z = pAccelerationValue->z;
	*/
}
